﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime.Tree;
using Antlr.Runtime;

namespace NeuroSpeech.Imap
{
	public class BaseImapRequest<T>
		where T:BaseImapResponse
	{
		T _Response = default(T);

		public T Response {
			get {
				return _Response;
			}
		}

		internal void Process(ImapClient client)
		{
			string cmdID = "ns" + client.NewCommandID();

			WriteCommand(cmdID, client);
			_Response = Activator.CreateInstance<T>();
			_Response.CommandID = cmdID.ToLower();
			_Response.Process(client);
			PostProcess(client, _Response);
			_Response.PostProcess(client);
		}

		#region private void PostProcess(ImapClient client,T _Response)
		protected virtual void PostProcess(ImapClient client, T response)
		{
			
		}
		#endregion

		#region private void WriteCommand(string cmdID,ImapClient client)
		protected virtual void WriteCommand(string cmdID, ImapClient client)
		{
			client.WriteLine(cmdID + " " + this.ToString());
		}
		#endregion

	}

}
